package cn.hs.ph.servlet;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.dao.impl.ShopDaoImpl;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,String> map = new TreeMap<>();
        ShopDao shopDao = new ShopDaoImpl();
        String action = req.getParameter("action");
        if("select".equals(action)){
            String keyword = req.getParameter("keyword");
            String phone = req.getParameter("phone");
            String shopStatus = req.getParameter("shopStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("keyword",keyword);
            map.put("phone",phone);
            map.put("shopStatus",shopStatus);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            List<Shop> list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);
            Map<String,Object> resulemap = new HashMap<>();
            resulemap.put("list",list);
            resulemap.put("total",total);
            Result<Shop> result = Result.success(resulemap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)){
            //前端响应数据
            String flag = req.getParameter("addOrUpdateFlag");
            int addOrUpdateFlag = 0;
            if(flag!=null && !"".equals(flag)){
                addOrUpdateFlag = Integer.parseInt(flag);
            }
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String id = req.getParameter("shopId");
            int shopId = 0;
            if (id!=null && !"".equals(id)){
                shopId = Integer.parseInt(id);
            }

            Shop shop = new Shop(shopAddress,shopCity,shopDescribe,shopDistrict,shopImage,shopLatitude,shopLongitude,shopName,shopPhone,shopProvince,shopId);
            if(addOrUpdateFlag==1){//新增
                int num = shopDao.insert(shop);
                Result result = null;
                if(num!=0){
                    result = Result.success("添加成功");
                }else {
                    result = Result.error("添加失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }else {//修改

                int num = shopDao.update(shop);
                Result result = null;
                if(num!=0){
                    result = Result.success("修改成功");
                }else {
                    result = Result.error("修改失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if ("delete".equals(action)){
            String id = req.getParameter("shopId");
            int shopId = Integer.parseInt(id);
            int num = shopDao.delete(shopId);
            Result result = null;
            if(num!=0){
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }
}
